﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Office.Interop.Word;
using System.Web.Mvc;
namespace LY.PdfToWord
{
    public class WriteIntoWord 
    {
        private ApplicationClass app = null; //定义应用程序对象 
        private Document doc = null; //定义 word 文档对象
        private Object missing = System.Reflection.Missing.Value; //定义空变量
        private Object isReadOnly = false;
        // 向 word 文档写入数据 
        public void OpenDocument(string FilePath)
        {
            object filePath = FilePath; //文档路径
            app = new ApplicationClass(); //打开文档 
            doc = app.Documents.Open(ref filePath, ref missing, ref missing, ref missing,
                                     ref missing, ref missing, ref missing, ref missing);
            doc.Activate(); //激活文档
        }
        /// <summary> 
        ///   打开word，将对应数据写入word里对应书签域 
        /// </summary> 
        ///<param name="parLableName">域标签</param> 
        /// <param name="parFillName">写入域中的内容</param>  

        public void WriteIntoDocument(string BookmarkName, string FillName)
        {
            object bookmarkName = BookmarkName;
            Bookmark bm = doc.Bookmarks.get_Item(ref bookmarkName); //返回书签 
            bm.Range.Text = FillName; //设置书签域的内容
        }

        /// <summary> 
        /// 保存并关闭 
        /// </summary> 
        /// <param name="parSaveDocPath">文档另存为的路径</param>
        /// 
        public void Save_CloseDocument(string SaveDocPath)
        {
            object savePath = SaveDocPath; //文档另存为的路径 
            Object saveChanges = app.Options.BackgroundSave; //文档另存为 
            doc.SaveAs(ref savePath, ref missing, ref missing, ref missing, ref missing,
                       ref missing, ref missing, ref missing);
            doc.Close(ref saveChanges, ref missing, ref missing); //关闭文档
            app.Quit(ref missing, ref missing, ref missing); //关闭应用程序

        }

        /// <summary>

        /// 把Word文件转换成pdf文件

        /// </summary>

        /// <param name="sourcePath">需要转换的文件路径和文件名称</param>

        /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>

        /// <returns>成功返回true,失败返回false</returns>

        public bool WordToPdf(object sourcePath, string targetPath)
        {

            bool result = false;

            WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;

            object missing = Type.Missing;

            Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;

            Document document = null;

            try
            {

                applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();

                document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                if (document != null)
                {

                    document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);

                }

                result = true;

            }

            catch
            {

                result = false;

            }

            finally
            {

                if (document != null)
                {

                    document.Close(ref missing, ref missing, ref missing);

                    document = null;

                }

                if (applicationClass != null)
                {

                    applicationClass.Quit(ref missing, ref missing, ref missing);

                    applicationClass = null;

                }

            }

            return result;

        }
    }
}
